package com.example.whereismystuff.views;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.ItemDataManager;
import com.example.whereismystuff.classes.LoginDataBaseAdapter;
import com.example.whereismystuff.classes.UserDataManager;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

/**
 * This is the view that gets called immediatly after logging in. It is essentially the homescreen of the application 
 * and gives the user four different choices. View my items, view all items, search, and merge items.
 * @author NullPointers
 *
 */
public class DashboardView extends Activity {
	ImageButton myItems, allItems, search, merge;
	AlertDialog.Builder alertBox;
	Intent i;

	String username, password;

	UserDataManager userManager;
	LoginDataBaseAdapter loginDataBaseAdapter;
	public static final String APP_ID = "165121346976660";    
	public Facebook facebook = new Facebook(APP_ID);
	
	Button logOutButton;

	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard);

		myItems = (ImageButton) findViewById(R.id.myItemsButtons);
		//usernameInput = (EditText) findViewById(R.id.a_usernameInput);
		//passwordInput = (EditText) findViewById(R.id.a_passwordInput);
		//alertBox = new AlertDialog.Builder(this);
		
		//logOutButton = (Button) findViewById(R.id.logOutButton);
	}
	
	/**
	 * Creates the menu
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.dashboard_menu, menu);
		return (super.onCreateOptionsMenu(menu));
	}

	/**
	 * Tells which method to call based on the pressed menu item
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.postItem:
		{
			try{
				Class c = Class.forName("com.example.whereismystuff.views.ItemViewer");
				//i = new Intent(getApplicationContext(), c);
				i = getIntent();
				Bundle extras = i.getExtras();
				extras.putString("calling-activity", "myitems");
				//i.putExtra("calling-activity", "myitems");
				i = new Intent(getApplicationContext(), c);
				i.putExtras(extras);
				startActivity(i);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
			return true;
		case R.id.logOutDashboard:
		case R.id.logOut2:
			logOut();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	
	/**
	 * Logs out the User
	 */
	public void logOut()
	{
		try{
			if(facebook.isSessionValid()){
				try {
					facebook.logout(this);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Class c = Class.forName("com.example.whereismystuff.views.Login");
				Intent i = new Intent(getApplicationContext(), c);
				startActivity(i);
				finish();
			}
			else{
			Class c = Class.forName("com.example.whereismystuff.views.Login");
			Intent i = new Intent(getApplicationContext(), c);
			startActivity(i);
			finish();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * This method restricts the user from pressing hte back button. THe only way to go back to the login screen is 
	 * pressing the menu button and logging out.
	 */
	public void onBackPressed() {
		return;
	}
	
	
	/**
	 * This method redirects the user to item viewer with a string passed along with the bundle to 
	 * notify Itemviewer which was the calling activity. In this case it would pass my items and only show the 
	 * items for the current user logged in.
	 * @param v
	 */
	public void onMyItems(View v) {
		try{
			Class c = Class.forName("com.example.whereismystuff.views.ItemViewer");
			//i = new Intent(getApplicationContext(), c);
			i = getIntent();
			Bundle extras = i.getExtras();
			extras.putString("calling-activity", "myitems");
			//i.putExtra("calling-activity", "myitems");
			i = new Intent(getApplicationContext(), c);
			i.putExtras(extras);
			startActivity(i);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method redirects the user to item viewer with a string passed along with the bundle to 
	 * notify Itemviewer which was the calling activity. In this case it would pass all items and show
	 * every item currently in the database
	 * @param v
	 */
public void onAllItems(View v) {
	try{
		Class c = Class.forName("com.example.whereismystuff.views.ItemViewer");
		//i = new Intent(getApplicationContext(), c);
		i = getIntent();
		Bundle extras = i.getExtras();
		extras.putString("calling-activity", "all-items");
		//i.putExtra("calling-activity", "myitems");
		i = new Intent(getApplicationContext(), c);
		i.putExtras(extras);
		startActivity(i);
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
}
/**
 * This method redirects the user to search view when the button is pressed on the home screen
 * @param v
 */
public void onSearch(View v) {
	try{
		Class c = Class.forName("com.example.whereismystuff.views.SearchView");
		//i = new Intent(getApplicationContext(), c);
		i = getIntent();
		Bundle extras = i.getExtras();
		extras.putString("calling-activity", "all-items");
		//i.putExtra("calling-activity", "myitems");
		i = new Intent(getApplicationContext(), c);
		i.putExtras(extras);
		startActivity(i);
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
}

/**
 * This method redirects the user to the merge view when the button is pressed. 
 * @param v
 */
public void onMerge(View v) {
	try{
		Class c = Class.forName("com.example.whereismystuff.views.MergeView");
		//i = new Intent(getApplicationContext(), c);
		i = getIntent();
		Bundle extras = i.getExtras();
		extras.putString("calling-activity", "all-items");
		//i.putExtra("calling-activity", "myitems");
		i = new Intent(getApplicationContext(), c);
		i.putExtras(extras);
		startActivity(i);
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	}
}
	
		
	}
